package com.hducms.common.util;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TreeMap;

import org.json.JSONArray;

import com.hducms.common.util.json.JSONException;
import com.hducms.common.util.json.JSONObject;

import open189.sign.ParamsSign;
public class DynamicPasswordTool {

//	static String appId = "346365170000035480";
//	static String appSecret = "6e32f22aba1c8bf1caf8c537bbb1cd5a";
//	static String accessToken = "638542cd879245af79a400fb0788e36e1398423270789";
	public String appId;
	public String appSecret;
	public String accessToken;
	public  String sendSms(String userPhone,String randcode) throws Exception {
		Date date = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String timestamp = dateFormat.format(date);
		System.err.println(timestamp);
		TreeMap<String, String> paramsMap = new TreeMap<String, String>();
		paramsMap.put("app_id", appId);
		paramsMap.put("access_token", accessToken);
		paramsMap.put("timestamp", timestamp);
		
		String getUrl = "http://api.189.cn/v2/dm/randcode/token?app_id=" + appId
				+ "&access_token=" + accessToken + "&timestamp="+timestamp + "&sign="+ParamsSign.value(paramsMap, appSecret);
		String resJson = HttpInvoker.httpGet(getUrl);
		System.err.println(resJson);
		JSONObject json = new JSONObject(resJson);
		System.out.println(json.get("token"));

		TreeMap<String, String> paramsMap1 = new TreeMap<String, String>();
		paramsMap1.put("app_id", appId);
		paramsMap1.put("access_token", accessToken);
		paramsMap1.put("timestamp", timestamp);
		paramsMap1.put("token", json.get("token").toString());
		paramsMap1.put("phone", userPhone);
		paramsMap1.put("exp_time", "2");
		paramsMap1.put("randcode", randcode);
		
		String postUrl = "http://api.189.cn/v2/dm/randcode/sendSms?app_id="
		                 + appId + "&access_token=" + accessToken + "&timestamp=" + timestamp + "";
		String postEntity = "token=" + json.get("token")
				          + "&phone=" + userPhone
				          + "&randcode=" + randcode
				          + "&exp_time=" + "2"
				          + "&sign="+ParamsSign.value(paramsMap1, appSecret);
		
		String resJson1 = HttpInvoker.httpPost(postUrl,null,postEntity);
		System.out.println(resJson1);
		return getIdentifier(resJson1);
	}
	public String getIdentifier(String jsonString) {
		JSONObject jsonObject;
		String identifier="";
		try {
			jsonObject = new JSONObject(jsonString);
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
		try {
			identifier = jsonObject.getString("identifier");
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
		return identifier;
	}
	
	
	public String getAppId() {
		return appId;
	}
	public void setAppId(String appId) {
		this.appId = appId;
	}
	public String getAppSecret() {
		return appSecret;
	}
	public void setAppSecret(String appSecret) {
		this.appSecret = appSecret;
	}
	public String getAccessToken() {
		return accessToken;
	}
	public void setAccessToken(String accessToken) {
		this.accessToken = accessToken;
	}
	
}
